/**
 * 
 */
package com.neal.web.validation;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.neal.domain.securities.User;


/**
 * @author 炜
 * @version 1.0
 * @description 
 */
public class UserValidator implements Validator{

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@Override
	public boolean supports(Class<?> clazz) {
		return clazz.isAssignableFrom(User.class);
	}

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors, "userId", "user.id.null");
		ValidationUtils.rejectIfEmpty(errors, "userName", "user.name.null");
		
		if(!errors.hasErrors()){
			User user = (User)target;
			if(user.getUserName().length() < 3){
				errors.reject("length.lessThan",new Object[]{"user.name",3},"length.lessThan");
			}
		}
	}

}
